<?php
require_once('../../includes/classes/core.php');

$action	= $_REQUEST['act'];
$error	= '';
$data	= '';

switch ($action) {
	case 'get_edit_page1':
		
 		$mac_id	 = $_REQUEST['id'];
 		$page		 = GetPage();
 		$data		 = array('page'	=> $page);

 		break;
	case 'get_list':
		$count		= $_REQUEST['count'];
		$hidden		= $_REQUEST['hidden'];
		$object_id	= $_REQUEST['object_id'];
		$status_id	= $_REQUEST['status_id'];
		
		
		if ($object_id != 0 && $status_id !=0) { $fillter="WHERE  `status`.id=$status_id AND monitoring_computer_state.ip = '$object_id'";}
		else if ($object_id != 0 && $status_id ==0) { $fillter="WHERE  monitoring_computer_state.ip = '$object_id'";}
		else if ($object_id == 0 && $status_id !=0) { $fillter="WHERE  `status`.id = $status_id";}
		else  { $fillter="WHERE `status`.`status`!=''";}
			
		$rResult = mysql_query("SELECT 	monitoring_computer_state.ip,
										IF(object.object_name='', object.ip, object.object_name),
										(SELECT COUNT(monit_st.`ip`)
										 FROM   monitoring_computer_state AS monit_st
										 WHERE  monit_st.`ip` = monitoring_computer_state.ip
 										) AS komp_raod,
			
										
										(SELECT concat('<span style=\"font-weight: 900;\">',COUNT(monit_st.`ip`),'</span>')
										 FROM   monitoring_computer_state AS monit_st
										 WHERE  monit_st.`status` = 2 
										 AND monit_st.ip = monitoring_computer_state.ip
 										 AND    (UNIX_TIMESTAMP(NOW()) - monit_st.live_datetime  <= 20)
 										) AS moqmedis_raodenoba,
				
										
										(SELECT concat('<span style=\"font-weight: 900;\">',COUNT(monit_st.`ip`),'</span>')
										 FROM   monitoring_computer_state AS monit_st
										 WHERE  monit_st.`status` = 3 
										 AND    monit_st.ip = monitoring_computer_state.ip
 										 AND    (UNIX_TIMESTAMP(NOW()) -  monit_st.live_datetime <= 20)
 										) AS umoqmedos_raodenoba,
										concat('<span style=\"font-weight: 900;\">',
												SEC_TO_TIME(UNIX_TIMESTAMP(NOW())-(SELECT MIN(monit_tsate.datetime)
																				    FROM monitoring_computer_state AS monit_tsate
																				    WHERE monit_tsate.ip = monitoring_computer_state.ip
																				    AND    (UNIX_TIMESTAMP(NOW()) - monit_tsate.live_datetime ) <= 20  
																				    AND monit_tsate.`status`=3)),
												'</span>'),
				
										
										(SELECT concat('<span style=\"font-weight: 900;\">',COUNT(mn_st.ip),'</span>')
										 FROM   monitoring_computer_state AS mn_st
										 WHERE 	mn_st.ip=monitoring_computer_state.ip
										 AND 	(UNIX_TIMESTAMP(NOW()) - mn_st.live_datetime > 20)
 										) AS gamortulis_raodenoba,
										concat('<span style=\"font-weight: 900;\">',
												SEC_TO_TIME(UNIX_TIMESTAMP(NOW())-(SELECT MIN(monit_tsate.datetime)
																				   FROM monitoring_computer_state AS monit_tsate
																				   WHERE monit_tsate.ip = monitoring_computer_state.ip
																				   AND    (UNIX_TIMESTAMP(NOW()) - monit_tsate.live_datetime) > 20)),
												'</span>')
										 
				
										
										
								FROM       monitoring_computer_state
								LEFT JOIN  object ON object.ip = monitoring_computer_state.ip
								LEFT JOIN  `status` ON monitoring_computer_state.`status` = `status`.id
								LEFT JOIN  `url_name` ON monitoring_computer_state.`last_url` = `url_name`.url
								$fillter
								GROUP BY monitoring_computer_state.ip");

		$data = array(
				"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) ){
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ ){
 				if($i==3){
 					$row[]='<div style="float: left; background: #E89B00; height: 100%; width: 120%; margin: 0px -9px; text-align: center">'.$aRow[$i].'</div>';
 				}elseif ($i==4){
					$row[]='<div style="float: left; background: #52E800; height: 100%; width: 120%; margin: 0px -9px; text-align: center">'.$aRow[$i].'</div>';
				}elseif ($i==5){
					$row[]='<div style="float: left; background: #52E800; height: 100%; width: 123%; margin: 0px -9px; text-align: center">'.$aRow[$i].'</div>';
				}elseif ($i==6){
					$row[]='<div style="float: left; background: #808080; height: 100%; width: 120%; margin: 0px -9px; text-align: center">'.$aRow[$i].'</div>';
				}elseif ($i==7){
					$row[]='<div style="float: left; background: #808080; height: 100%; width: 122%; margin: 0px -9px; text-align: center">'.$aRow[$i].'</div>';
				}else{
 					$row[] = $aRow[$i];
				}
				
			}
			$data['aaData'][] = $row;
		}

		break;
		
	default:
		$error = 'Action is Null';
}
$data['error'] = $error;
echo json_encode($data);


/* 
 ******************************
 * Monitoring Functions
 * ******************************
*/
function get_button_name($mac_id){
	$res = mysql_fetch_assoc(mysql_query("SELECT  monitoring_computer_state.screenshot_enabled
										  FROM monitoring_computer_state
										  WHERE monitoring_computer_state.mac='$mac_id'"));
	if ($res['screenshot_enabled']==0){
		$dutton_name='სკრინშოტის ჩართვა';
	}else{
		$dutton_name='სკრინშოტის გამორთვა';
	}
	
	return $dutton_name;
}


function GetPage($res = ''){
	
	
	$data = '
			<div id="dialog-form" class="inner-table" style="width: 100%; margin: 0 auto;">
			<table class="display" id="example_object">
                    <thead >
                        <tr id="datatable_header">
                            <th>ID</th>
							 <th style="width: 35%;">ფილიალი</th>
                            <th style="width: 35%;">კომპიუტერის სახელი</th>
                            <th style="width: 180px;">კომპიუტერის სტატუსი</th>
                            <th style="width: 180px;">დრო</th>
                            <th style="width: 30%;">ლინკი</th>
                         </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
							<th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 115px;"/>
                            </th>
                            <th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 115px;"/>
                            </th>
                            <th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 115px;"/>
                            </th>
                            <th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 115px;"/>
                            </th>
                            <th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 115px;"/>
                            </th>
                            <th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 115px;"/>
                            </th>
                         </tr>
                    </thead>
                </table>
		</div>
	';
	return $data;
}
?>
